package us.sunrisemorning.pixivbrower.spider;

import com.jfinal.aop.Aop;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;
import us.sunrisemorning.pixivbrower.dao.ConfigService;
import us.sunrisemorning.pixivbrower.model.Config;

public class PixivProcessor implements PageProcessor {
    
    private ConfigService configService = Aop.get(ConfigService.class);
    
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
    
    private SpiderStatus status;
    
    public PixivProcessor(SpiderStatus status) {
        this.status = status;
    }

    public void process(Page page) {
        page.putField("result", new JsonPathSelector("$.body.illustManga.data").select(page.getRawText()));

        String total = new JsonPathSelector("$.body.illustManga.total").select(page.getRawText());
        status.setTotal(total);

//        page.addTargetRequests(page.getHtml().$("ul.page-list a.gtm-search-pager-bottom", "href").all());
    }

    public Site getSite() {
        site.addHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36");
        return site;
    }
}
